自動応答
前回のガイドを経て、私たちの創造物は単純なボットから洗練された自律エージェントへと進化しました。今回は、カウンターアタック機能を追加してその能力をさらに強化し、相手の攻撃に対して即座に反撃できるようにします。これにより、相手が安全な場所に逃げる前に驚かせる可能性があります。
コードを書く
次のハンドラーをbot.lua
ファイルに追加すれば準備完了です。
lua
-- Handler to automatically attack when hit by another player.
Handlers.add(
"ReturnAttack",
{ Action = "Hit" },
function (msg)
local playerEnergy = LatestGameState.Players[ao.id].energy
if playerEnergy == undefined then
print("Unable to read energy.")
ao.send({Target = Game, Action = "Attack-Failed", Reason = "Unable to read energy."})
elseif playerEnergy == 0 then
print("Player has insufficient energy.")
ao.send({Target = Game, Action = "Attack-Failed", Reason = "Player has no energy."})
else
print("Returning attack.")
ao.send({Target = Game, Action = "PlayerAttack", Player = ao.id, AttackEnergy = tostring(playerEnergy)})
end
InAction = false
ao.send({Target = ao.id, Action = "Tick"})
end
)
プレイヤーが攻撃されると、Action Hit
のメッセージを受け取ります。この設定により、エージェントは十分なエネルギーがある限り迅速にカウンターアタックを行うことができます。
最新のbot.lua
コードを以下のドロップダウンで参照できます。
Updated bot.lua file
lua
LatestGameState = LatestGameState or nil
function inRange(x1, y1, x2, y2, range)
return math.abs(x1 - x2) <= range and math.abs(y1 - y2) <= range
end
function decideNextAction()
local player = LatestGameState.Players[ao.id]
local targetInRange = false
for target, state in pairs(LatestGameState.Players) do
if target ~= ao.id and inRange(player.x, player.y, state.x, state.y, 1) then
targetInRange = true
break
end
end
if player.energy > 5 and targetInRange then
print("Player in range. Attacking.")
ao.send({Target = Game, Action = "PlayerAttack", Player = ao.id, AttackEnergy = tostring(player.energy)})
else
print("No player in range or insufficient energy. Moving randomly.")
local directionMap = {"Up", "Down", "Left", "Right", "UpRight", "UpLeft", "DownRight", "DownLeft"}
local randomIndex = math.random(#directionMap)
ao.send({Target = Game, Action = "PlayerMove", Player = ao.id, Direction = directionMap[randomIndex]})
end
end
Handlers.add(
"HandleAnnouncements",
{ Action = "Announcement" },
function (msg)
ao.send({Target = Game, Action = "GetGameState"})
print(msg.Event .. ": " .. msg.Data)
end
)
Handlers.add(
"UpdateGameState",
{ Action = "GameState" },
function (msg)
local json = require("json")
LatestGameState = json.decode(msg.Data)
ao.send({Target = ao.id, Action = "UpdatedGameState"})
end
)
Handlers.add(
"decideNextAction",
{ Action = "UpdatedGameState" },
function ()
if LatestGameState.GameMode ~= "Playing" then
return
end
print("Deciding next action.")
decideNextAction()
end
)
Handlers.add(
"ReturnAttack",
{ Action = "Hit" },
function (msg)
local playerEnergy = LatestGameState.Players[ao.id].energy
if playerEnergy == undefined then
print("Unable to read energy.")
ao.send({Target = Game, Action = "Attack-Failed", Reason = "Unable to read energy."})
elseif playerEnergy == 0 then
print("Player has insufficient energy.")
ao.send({Target = Game, Action = "Attack-Failed", Reason = "Player has no energy."})
else
print("Returning attack.")
ao.send({Target = Game, Action = "PlayerAttack", Player = ao.id, AttackEnergy = tostring(playerEnergy)})
end
InAction = false
ao.send({Target = ao.id, Action = "Tick"})
end
)
ローディングとテスト
カウンターアタック機能を有効化しテストするために、aosプレイヤーターミナルでボットファイルをロードします。
lua
.load bot.lua
ターミナルで自律エージェントの反応を観察してください。今や、即座に反撃する能力が追加されています。この機能は、エージェントの進化した戦略的深さと自律性を示しています。次のセクションでは、これまでに集めたすべての知識を統合し、最適化のための機能を追加します。